package jsc.base;
import java.io.*;

/**
 * @author Marc Gaëtano
 */

/**
 * The class <tt>TextFileWriter</tt> is a convenience class for writing text
 * files. The file to write is specified by a <tt>File</tt> object. An instance of
 * <tt>TextFileWriter</tt> provides a buffered writer so as to provide for the
 * efficient writing of strings.<p>
 *
 * The effective writing of strings into the actual file is not specified and may
 * occur at any time during the writing process. For the writing of string to be
 * effective and complete, the text file writer must be closed at the end of
 * use.<p>
 *
 * All the public methods become invalid after the text file writer has
 * been closed.
 */
public class TextFileWriter {

    private BufferedWriter bw; // the buffered writer

    /**
     * Create a buffered text file writer to write into a file. The initial
     * content of the file is lost.
     * @param f the file to write into.
     * @throws IOException if the specified file is not found or if some other I/O
     * error occurs.
     */
    public TextFileWriter(File f) throws IOException {
        bw = new BufferedWriter(new FileWriter(f));
    }

    /**
     * Write a string into the text file writer.
     * @param s the string to be written.
     * @throws IOException if the text file writer is closed or if an I/O
     * error occurs.
     */
    public void write(String s) throws IOException {
        bw.write(s);
    }

    /**
     * Write a line separator into the text file writer. Note that the line
     * separator string is not necessarily a single newline ('\n') character
     * and then <code>newLine()</code> may be different from
     * <code>write("\n")<\code>.
     * @throws IOException if the text file writer is closed or if an I/O
     * error occurs.
     */
    public void newLine() throws IOException {
        bw.newLine();
    }

    /**
     * Close the text file writer. Before closing complete the writing of buffered
     * strings into the actual file. The text file writer must be closed at the
     * end of use.
     * @throws IOException if the text file writer is already closed or if
     * an I/O error occurs.
     */
    public void close() throws IOException {
        bw.close();
    }
}
